import { reactive } from "vue";


// 表单数据接口规范
interface ForgetForm {
    email: string;
    password: string;
    checkPassword: string
}

// 表单数据
export let forgetForm = reactive<ForgetForm>({
    email: "",
    password: "",
    checkPassword: ""
});

// 初始化表单
export const initForgetForm = () => {
    // forgetForm.email = ''
    // forgetForm.password = ''
    // forgetForm.checkPassword = ''
    forgetForm = {
        email: "",
        password: "",
        checkPassword: ""
    }
}



// 表单校验 接口规范
interface ForgetRules {
    email: ({
        required: boolean;
        message: string;
        trigger: string;
        type?: undefined;
    } | {
        type: string;
        message: string;
        trigger: string;
        required?: undefined;
    })[];
    password: ({
        trigger: string;
        message: string;
        min?: undefined | number;
        max?: undefined | number;
    } | {
        required: boolean;
        message: string;
        trigger: string;
        type?: undefined;
    })[];
    checkPassword: ({
        required: boolean;
        trigger: string;
        message: string;
        min?: undefined;
        max?: undefined;
        validator?: undefined;
    } | {
        min: number;
        max: number;
        trigger: string;
        message: string;
        required?: undefined;
        validator?: undefined;
    } | {
        validator: (rules: any, val: string, callback: any) => void;
        trigger: string;
        required?: undefined;
        message?: undefined;
        min?: undefined;
        max?: undefined;
    })[]
}


// 确认密码校验
const checkIsSame = (rules: any, val: string, callback: any) => {
    if (val.trim() === "") {
        callback(new Error("请再次输入密码"));
    } else if (val !== forgetForm.password) {
        callback(new Error("两次输入密码不一致!"));
    } else {
        callback();
    }
};

// 表单校验规则
export const forgetRules = reactive<ForgetRules>({

    email: [
        { required: true, message: "请输入邮箱地址", trigger: "blur" },
        { type: "email", message: "请输入正确的邮箱地址", trigger: "blur" }
    ],
    password: [
        {
            min: 6,
            max: 20,
            trigger: "blur",
            message: "密码长度在6～20之间"
        },
        {
            required: true,
            trigger: "blur",
            message: "密码为必填"
        },
    ],
    checkPassword: [
        {
            required: true,
            trigger: "blur",
            message: "确认密码为必填"
        },
        {
            min: 6,
            max: 20,
            trigger: "blur",
            message: "密码长度在6～20之间"
        },
        {
            validator: checkIsSame,
            trigger: "blur"
        },
    ],
});


